package com.wlz.service.impl;

import com.github.javafaker.Faker;
import com.wlz.bean.*;
import com.wlz.mapper.*;
import com.wlz.service.IFakeDataService;
import com.wlz.utils.ResultBody;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

@Service
@Primary
public class FakeDataServiceImpl implements IFakeDataService {

    @Resource
    PersonMapper personMapper;
    @Resource
    CompanyMapper companyMapper;
    @Resource
    WzMapper wzMapper;
    @Resource
    HomeMapper homeMapper;
    @Resource
    SchoolMapper schoolMapper;
    @Resource
    FamilyMapper familyMapper;

    @Resource
    JygjMapper jygjMapper;
    @Resource
    EmployeeMapper employeeMapper;
    @Resource
    WzgxMapper wzgxMapper;
    @Resource
    JxMapper jxMapper;
    @Resource
    XxllMapper xxllMapper;
    @Resource
    JrgxMapper jrgxMapper ;
    @Resource
    YwgxMapper ywgxMapper ;

    @Override
    public void addPerson() {
        for (int a = 0; a < 100; a++){
            PersonBean personBean = new PersonBean();
            ArrayList<PersonBean> lp = new ArrayList<>(10000);
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                Faker faker = new Faker(Locale.CHINA);
                Random r =new Random();
                //personBean.setPersonNum(faker.idNumber().valid());
                String ssnIdcode = faker.idNumber().valid();

                personBean.setName(faker.name().fullName());
                personBean.setSsn(ssnIdcode);
                personBean.setSex(String.valueOf(r.nextInt(2)));
                personBean.setBirth(Integer.parseInt(faker.date().birthday().toInstant().toString().substring(0,10).replace("-","")));
                personBean.setNation(faker.nation().capitalCity());
                personBean.setIdType(String.valueOf(r.nextInt(3)));
                personBean.setIdcode(ssnIdcode);
                personBean.setAddress(faker.address().fullAddress());
                personBean.setPhone(faker.phoneNumber().phoneNumber());
                personBean.setMail(String.valueOf(r.nextInt(11)));
                personBean.setPoliticsStatus(String.valueOf(r.nextInt(3)));
                personBean.setGraduateIns(String.valueOf(r.nextInt(4)));
                personBean.setEduBck(String.valueOf(r.nextInt(4)));
                personBean.setLevel(String.valueOf(r.nextInt(5)));
                //personBean.setAge(String.valueOf(r.nextInt(100)));
                //personBean.setMajor(faker.book().title());
                //personBean.setGraduationDate(faker.date().birthday().toLocaleString().substring(0,9));
                //personBean.setLongitude(String.valueOf(r.nextInt(7)));
                //personBean.setLatitude(String.valueOf(r.nextInt(7)));
                //System.out.println(personBean);
                personMapper.insert(personBean);
            }
            personMapper.insertList(lp);
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
            lp.clear();
/*            try {
                Thread.sleep(1000*60*3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        }
    }
    @Override
    public void addCompany() {
        long startTime = System.currentTimeMillis();
        //for (int a = 0; a < 100; a++){
            CompanyBean companyBean = new CompanyBean();
            ArrayList<CompanyBean> lp = new ArrayList<>(200);
            for (int i = 0; i < 200; i++) {
                Faker faker = new Faker(Locale.CHINA);
                Random r =new Random();

                //companyBean.setCompanyId(String.valueOf(r.nextInt(100)+1));
                companyBean.setCompanyName("公司");
                companyBean.setLicenseNum("1");
                companyBean.setBussiness(String.valueOf(r.nextInt(100)));
                companyBean.setCompanyType(String.valueOf(r.nextInt(100)+1));
                companyBean.setPhone(faker.phoneNumber().phoneNumber());
                companyBean.setLegalPerson(faker.name().fullName());
                companyBean.setLegalPersonType(String.valueOf(r.nextInt(3)));
                companyBean.setLegalPersonId(faker.idNumber().valid());
                companyBean.setLegalPersonCell(faker.phoneNumber().cellPhone());
                companyBean.setLegalPersonPhone(faker.phoneNumber().phoneNumber());
                companyBean.setRegAddress(faker.address().fullAddress());
                companyBean.setOrgType(String.valueOf(r.nextInt(100)));
                companyBean.setOrgCode(faker.code().asin());
                companyBean.setRegNum(faker.number().digit());
                companyBean.setCerAuth(String.valueOf(r.nextInt(3)+1));
                companyBean.setEstablishDate(Integer.parseInt(faker.date().birthday().toInstant().toString().substring(0,10).replace("-","")));
                companyBean.setMail(String.valueOf(r.nextInt(11)));
                companyMapper.insert(companyBean);
            }
            companyMapper.insertList(lp);
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
            System.out.println("程序运行时间：" + (double) (endTime - startTime) / 1000 + "s");
            lp.clear();
            /*try {
                Thread.sleep(1000*60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        //}
    }
    @Override
    public void addWz() {
        ArrayList<WzBean> lp = new ArrayList<>(10000);
            for (int m = 1; m < 21; m++) {
                for (int n = 1; n < 21; n++) {
                    WzBean wzBean = new WzBean();
                    wzBean.setJd(String.valueOf(m));
                    wzBean.setWd(String.valueOf(n));
                    lp.add(wzBean);
                }
            }
        wzMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addHome() {
        Faker faker = new Faker(Locale.CHINA);
        ArrayList<HomeBean> lp = new ArrayList<>(10000);
        for (int m = 1; m < 201; m++) {
            HomeBean homeBean = new HomeBean();
            homeBean.setHomeName(faker.address().city());
            lp.add(homeBean);
        }
        homeMapper.insertList(lp);
        lp.clear();
        //insert into home2 (homename,homeid) select homename,MAX(homeid) from home1 GROUP BY homename
    }
    @Override
    public void addSchool() {
        Faker faker = new Faker(Locale.CHINA);
        ArrayList<SchoolBean> lp = new ArrayList<>(10000);
        for (int m = 1; m < 201; m++) {
            SchoolBean schoolBean = new SchoolBean();
            //schoolBean.setXxmc(faker.);
            //schoolBean.setZy();

            //homeBean.setHomeName(faker.address().city());
            lp.add(schoolBean);
        }
        schoolMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addFamily() {
        Faker faker = new Faker(Locale.CHINA);
        ArrayList<FamilyBean> lp = new ArrayList<>(10000);
        for (int i = 1; i < 10001; i++) {
            FamilyBean familyBean = new FamilyBean();

            familyBean.setName(faker.name().fullName());
            familyBean.setAddress(faker.address().streetAddress());

            lp.add(familyBean);
        }
        familyMapper.insertList(lp);
        lp.clear();

    }

    @Override
    public void test() {
        Faker faker = new Faker(Locale.CHINA);
        Random r = new Random();

        int num = 22;
        System.out.println(r.nextInt(3)+1);
        /*Date birthday = faker.date().birthday();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date d = null;
        try {
            d = sdf.parse("19950101");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Date dnow = new Date();

        //Date d1 = faker.date().between(d, dnow).toLocaleString();
        System.out.println(faker.date().between(d, dnow).toInstant().toString().substring(0,10).replace("-",""));*/
    }

    @Override
    public void addJygj() {
        JygjBean jygjBean = new JygjBean();
        ArrayList<JygjBean> lp = new ArrayList<>(10000);
        for (int i = 0; i < 10000; i++) {
            Faker faker = new Faker(Locale.CHINA);
            Random r =new Random();

            jygjBean.setPid(String.valueOf(r.nextInt(10000)+1));
            jygjBean.setCompanyId(String.valueOf(r.nextInt(200)+1));

            Date dnow = new Date();
            long l=Date.parse("Mon 6 Jan 1995 13:3:00");
            Date day=new Date(l);
            Date d1 = faker.date().between(day, dnow);
            Date d2 = faker.date().between(day, dnow);
            if (d1.after(d2)){
                jygjBean.setBeginTime(Integer.parseInt(d2.toInstant().toString().substring(0,10).replace("-","")));
                jygjBean.setEndTime(Integer.parseInt(d1.toInstant().toString().substring(0,10).replace("-","")));
            } else {
                jygjBean.setBeginTime(Integer.parseInt(d1.toInstant().toString().substring(0,10).replace("-","")));
                jygjBean.setEndTime(Integer.parseInt(d2.toInstant().toString().substring(0,10).replace("-","")));
            }

            jygjMapper.insert(jygjBean);
        }
        jygjMapper.insertList(lp);
        lp.clear();

    }
    @Override
    public void addEmployee() {

        ArrayList<EmployeeBean> lp = new ArrayList<>(10000);
        for (int i = 0; i < 10000; i++) {
            EmployeeBean employeeBean = new EmployeeBean();
            Random r =new Random();
            employeeBean.setCompanyId(String.valueOf(r.nextInt(200)+1));
            employeeBean.setPid(String.valueOf(r.nextInt(10000)+1));

            lp.add(employeeBean);
            //jygjMapper.insert(jygjBean);
        }
        employeeMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addWzgx() {
        ArrayList<WzgxBean> lp = new ArrayList<>(10000);

        int m = 0;
        int n = 0;
        for (int i = 1; i < 10201; i++) {
            ++m;
            ++n;
            if (n == 401){
                n = 0;
                continue;
            }
            WzgxBean wzgxBean = new WzgxBean();
            wzgxBean.setPid(String.valueOf(m));
            wzgxBean.setWzid(String.valueOf(n));
            lp.add(wzgxBean);

        }

        wzgxMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addJx() {
        //homeid 118
        ArrayList<JxBean> lp = new ArrayList<>(10000);

        int m = 0;
        int n = 0;
        for (int i = 1; i < 10201; i++) {
            ++m;
            ++n;
            if (n == 119){
                n = 0;
                continue;
            }
            JxBean jxBean = new JxBean();
            jxBean.setPid(String.valueOf(m));
            jxBean.setHomeId(String.valueOf(n));
            lp.add(jxBean);

        }
        jxMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addXxll() {
        ArrayList<XxllBean> lp = new ArrayList<>(10200);
        Faker faker = new Faker();
        Random r = new Random();

        int m = 0;
        int n = 0;
        for (int i = 1; i < 10201; i++) {
            ++m;
            ++n;
            if (n == 17){
                n = 0;
                continue;
            }
            int num = r.nextInt(10) + 7;

            XxllBean xxllBean = new XxllBean();
            xxllBean.setPid(String.valueOf(m));
            xxllBean.setId(String.valueOf(n));
            xxllBean.setLicenseId(faker.idNumber().valid());
            //19950901-19960701
            xxllBean.setBeginTime(Integer.parseInt("20"+num+"0901"));
            xxllBean.setEndTime(Integer.parseInt("20"+(num+4)+"0701"));
            xxllBean.setXl("本科");
            xxllBean.setXlxz("全日制");


            lp.add(xxllBean);

        }
        xxllMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addJrgx() {
        ArrayList<JrgxBean> lp = new ArrayList<>(10000);
        Faker faker = new Faker();
        Random r = new Random();

        int m = 0;
        int n = 0;
        for (int i = 1; i < 10001; i++) {
            ++m;
            ++n;
            if (n == 10001){
                break;
            }

            JrgxBean jrgxBean = new JrgxBean();
            jrgxBean.setPid(String.valueOf(m));
            jrgxBean.setJrid(String.valueOf(n));

            lp.add(jrgxBean);

        }
        jrgxMapper.insertList(lp);
        lp.clear();
    }
    @Override
    public void addYwgx() {
        ArrayList<YwgxBean> lp = new ArrayList<>(20847);

        int m = 0;
        int n = 0;
        for (int i = 1; i < 20847; i++) {
            ++m;
            ++n;
            if (n == 10201){
                n = 0;
                continue;
            }

            YwgxBean ywgxBean = new YwgxBean();
            ywgxBean.setPid(String.valueOf(n));
            ywgxBean.setYwsjid(String.valueOf(m));

            lp.add(ywgxBean);

        }
        ywgxMapper.insertList(lp);
    }


}




